package sz.szsmk.citizencard.util;

import android.os.CountDownTimer;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by yangzhengwen on 2017/10/2.
 */

public class SmsTimer extends CountDownTimer {

    /**
     * 获取验证码按钮对象
     */
    private TextView button;
    private EditText editText;
    private TextView modifyEditText;
    private long countDownInterval = 1000;

    /**
     * @param millisInFuture    定时总时间
     * @param countDownInterval 执行时间间隔
     * @param button            获取验证码按钮
     */
    public SmsTimer(long millisInFuture, long countDownInterval, TextView button) {
        super(millisInFuture - 1, countDownInterval);
        this.button = button;
        this.countDownInterval = countDownInterval;
    }

    public SmsTimer(long millisInFuture, long countDownInterval, TextView button, EditText editText, TextView modifyEditText) {
        super(millisInFuture - 1, countDownInterval);
        this.button = button;
        this.countDownInterval = countDownInterval;
        this.editText = editText;
        this.modifyEditText = modifyEditText;
    }

    public SmsTimer(long millisInFuture, long countDownInterval, TextView button, EditText editText) {
        super(millisInFuture - 1, countDownInterval);
        this.button = button;
        this.countDownInterval = countDownInterval;
        this.editText = editText;
    }

    @Override
    public void onFinish() {
        button.setClickable(true);
        button.setText("重新获取");
        if (editText != null) {
            editText.setEnabled(true);
        }
        if (modifyEditText != null)
            modifyEditText.setEnabled(true);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        button.setClickable(false);
        button.setText(millisUntilFinished / countDownInterval + "秒");
    }

    public void stop() {
        this.cancel();
        this.onFinish();
    }
}
